--[[

itemdata.lua

Routines for handling various functions related to the items themselves.

]]
-- Function: Return the ID when given the item link
function LBT_LinkToID(itemLink)
	if( itemLink ) then
		local _,_,id = string.find(itemLink, "^.*item:([0-9]+):.*$");	
		return id;
	end
	return nil;
end

-- Function: Find the tooltip line which contains itemString
function LBT_GetItemTooltipLine(itemBag, itemSlot, itemString)
	local myCheckLine;

	local itemIndex = 1;
	local tooltipLine = nil;

	LBT_ToolTip:SetOwner(WorldFrame,"ANCHOR_NONE");
	LBT_ToolTip:ClearLines();
	LBT_ToolTip:SetBagItem(itemBag, itemSlot);

	while (getglobal("LBT_ToolTipTextLeft"..itemIndex) ~= nil) do
		local myText = getglobal("LBT_ToolTipTextLeft"..itemIndex):GetText();

		if (myText ~= nil) then
			if (string.find(myText, itemString) ~= nil) then
				tooltipLine = myText;
				LBT_ToolTip:Hide();
				return tooltipLine;
			end
		end

		itemIndex = itemIndex + 1;
	end
	LBT_ToolTip:Hide();
	return tooltipLine;
end

-- Function: When given a bag, return the value of the items in that bag.
function LBT_BagValue(bag)

	if (bag == nil) then
		return;
	end

	local j;
	local size;
	local CashValue	= 0;
	local gold		= 0;
	local silver		= 0;
	local copper	= 0;
	--local itemValue;

	size = GetContainerNumSlots(bag);
	if( size and size > 0 ) then
		for j = 1, size do
			if (GetContainerItemInfo(bag, j)) then
				--itemValue = LBT_GetItemValue(bag,j);
				local _,itemCount				= GetContainerItemInfo(bag, j);
				local itemLink					= GetContainerItemLink(bag, j);
				local _, _, _, _, _, _, _, _, _, _, itemValue	= GetItemInfo(itemLink)
				if (itemValue == nil) then
					itemValue = 0
				else
					itemValue = itemValue * itemCount
				end
				if (itemValue > 0) then
					CashValue = CashValue + itemValue;
				end
			end
		end
	end

	if (CashValue >= 0) then
		gold	= floor(CashValue / (COPPER_PER_SILVER * SILVER_PER_GOLD));
		silver	= floor((CashValue - (gold * COPPER_PER_SILVER * SILVER_PER_GOLD)) / COPPER_PER_SILVER);
		copper	= mod(CashValue, COPPER_PER_SILVER);
	end

	return HIGHLIGHT_FONT_COLOR_CODE..format("%sg, %ss, %sc", gold, silver, copper)..FONT_COLOR_CODE_CLOSE;
end
